import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  Put,
  Query,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import {
  MessageService,
  MessageQueryParams,
  MessageListResponse,
} from './message.service';

@Controller('messages')
export class MessageController {
  constructor(private readonly messageService: MessageService) {}

  @Post()
  create(@Body() body) {
    return this.messageService.create(body);
  }

  @Get()
  async findAll(
    @Query() query: MessageQueryParams,
  ): Promise<MessageListResponse> {
    return this.messageService.findAll(query);
  }

  @Get('stats/temperature')
  async getTemperatureStats(
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
  ) {
    try {
      return await this.messageService.getTemperatureStats(startDate, endDate);
    } catch (error) {
      console.error('Error in getTemperatureStats:', error);
      throw new HttpException(
        'Internal server error',
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  @Get('stats/cable-temperature')
  async getCableTemperatureStats(
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
  ) {
    try {
      return await this.messageService.getCableTemperatureStats(
        startDate,
        endDate,
      );
    } catch (error) {
      console.error('Error in getCableTemperatureStats:', error);
      throw new HttpException(
        'Internal server error',
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  @Get('stats/discharge-max')
  async getDischargeAvgStats(
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
  ) {
    try {
      return await this.messageService.getDischargeMaxStats(startDate, endDate);
    } catch (error) {
      console.error('Error in getDischargeAvgStats:', error);
      throw new HttpException(
        'Internal server error',
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  @Get('stats/water-leak')
  async getWaterLeakStats(
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
  ) {
    try {
      return await this.messageService.getWaterLeakStats(startDate, endDate);
    } catch (error) {
      console.error('Error in getWaterLeakStats:', error);
      throw new HttpException(
        'Internal server error',
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  @Get(':id')
  async findOne(@Param('id') id: string) {
    try {
      const numericId = parseInt(id, 10);
      if (isNaN(numericId)) {
        throw new HttpException('Invalid ID format', HttpStatus.BAD_REQUEST);
      }

      const message = await this.messageService.findOne(numericId);
      if (!message) {
        throw new HttpException('Message not found', HttpStatus.NOT_FOUND);
      }

      return message;
    } catch (error) {
      console.error('Error in findOne:', error);
      if (error instanceof HttpException) {
        throw error;
      }
      throw new HttpException(
        'Internal server error',
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  @Put(':id')
  update(@Param('id') id: number, @Body() body) {
    return this.messageService.update(id, body);
  }

  @Delete(':id')
  remove(@Param('id') id: number) {
    return this.messageService.remove(id);
  }
}
